// var defaultAvatarUrl = require ("../../static/img/avatar.png")
// var variables = require ("../../styles/")
const api = require('../../config/api.js');
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // staticImg: {
    //   phone,
    //   defaultAvatarUrl
    // }
    valided:false,
    isLogin:false,
    wxUser:null,
    workSheetList:'',
    companyName:"暂无公司信息"
  },
  handleLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出当前账号？',
      confirmColor: variables.mainColor,
      success: res => {
        if (res.confirm) {
          
        } else if (res.cancel) {
        }
      }
    })
  },
  getUserinfo() {
    this.setData({
      isLogin:true
    })
  },
  goCompany() {
    let status = this.valided()
    if(status){
      wx.navigateTo({
        url: '/pages/company/company?userId='+this.data.wxUser.id
      });
    }  
  },
  /**
   * 跳转到个人基础信息
   */
  toBaseInfo(){
    let status = this.validedLogin()
    if(status){
      wx.navigateTo({
        url: '/pages/baseInfo/index?wxUser='+JSON.stringify(this.data.wxUser)
      })
    }
    
  },

  onAuthClose(){
    let user = wx.getStorageSync('wxUser') || {}
    this.setData({
      showProp:false,
      wxUser:user
    })
  },
  /**
   * 跳转到企业
   */
  toCompany(){
    let status = this.valided()
    if(status){
      wx.navigateTo({
        url: '/pages/company/index',
      })
    }
    
  },
  gomyList() {
    let status = this.valided()
    if(status){
      wx.navigateTo({
        url: '/pages/pickerhead/index?invoiceTitle='+this.data.wxUser.invoiceTitle+'&userId='+this.data.wxUser.id,
      })
    }
   
  },
  goPay() {
    let status = this.valided()
    if(status){
      wx.navigateTo({
        url: '/pages/pay/index',
      })
    }

  },
  validedLogin(){
    //判断是否登录
    if(this.data.wxUser == null){
      wx.showToast({
        title: '请先登录！',
        icon: 'none',
      })
      return false; 
    }
    return true;
  },
  valided() {
		this.setData({
			showProp: false
		})
    let hasUser = getApp().hasUser()
    console.log("hasUser status",hasUser) 
		if (!hasUser) {
			this.setData({
        showProp: true,
        onConfirmId:0
      })
      console.log("hasUser",this.data.onConfirmId)
      return false
		} else  {
      let hasPhone = getApp().hasPhone()
      console.log("hasPhone status",hasPhone) 
      if(!hasPhone){
        let user = wx.getStorageSync('wxUser')
        let wxName = user.wxName
        let wxUrl = user.wxUrl
        console.log("hasPhone user ",user)
        this.setData({
          showProp: true,
          onConfirmId:1,
          userInfo:{
            avatarUrl:wxUrl,
            nickName:wxName
          }
        })
        return false
      }
      console.log("hasPhone",this.data.onConfirmId) 
    }
    return true
	},
  // valided(){
  //   this.setData({
	// 		showProp:false
	// 	})
  //   let hasUser = getApp().hasUser()
	// 	if(!hasUser){
	// 		this.setData({
  //       showProp: true,
  //       valided:false
  //     })
  //     return false
	// 	}else{
	// 		this.setData({
  //       showProp: false,
  //       valided:true
  //     })
  //     return true
	// 	}
  // },
  /**
   * 获取个人信息
   */
  getUserProfile(e){
    var this_  = this;
    wx.showLoading()
    var param = {};
    param.desc = "用户信息展示"
    //调用微信api 获取个人信息
    wx.getUserProfile({
      desc:"用户信息展示",
      success:(res)=>{
        wx.hideLoading();
        console.log("userInfo",res.userInfo)
        //将个人信息 传给后台
        let userInfo = res.userInfo;
        userInfo.openid = this_.data.openid
        wx.request({
          url: api.saveBaseInfo,
          method:'POST',
          data:userInfo,
          success:(res)=>{
            wx.hideLoading();
            if(res.data.code == 200){
              if(res.data.code == 200){
                wx.setStorageSync('wxUser', res.data.wxUser)
                this_.setData({
                  wxUser : res.data.wxUser
                })
              }else{
                wx.showToast({
                  title: res.data.msg
                })
              }
            }
          },
        })
      },
      fail:(res)=>{
        wx.hideLoading();
      }
    })
  },
  goWorkList() {
    let status = this.valided()
    if(status){
      wx.navigateTo({
        url: '/pages/workList/index?userId='+this.data.wxUser.id
     });
    }
   
  },
 
  goCollectionList(){
    let status = this.valided()
    if(status){
      wx.navigateTo({
        url: '/pages/collection/index?userId='+this.data.wxUser.id
     });
    }
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取分享者id
    let sender = options.sender;
    // if(sender == null){
    //   sender = wx.getStorageSync('sender')
    // }
    if(sender!=null){
        this.setData({
          sender:sender
        })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    let this_ = this;
    var openid = wx.getStorageSync('openid');
    if(openid!=null && openid != ''){
      this.setData({
        openid:openid
      })
      wx.request({
        url: api.getBaseInfo,
        method:'GET',
        data:{
          openid:openid
        },
        success:(res)=>{
          if(res.data.code == 200){
            wx.setStorageSync('wxUser', res.data.wxUser)
            this_.setData({
              wxUser:res.data.wxUser,
              wxPhone:res.data.wxUser.wxPhone,
              addedServicesCount:res.data.addedServicesCount,
              collectCount:res.data.collectCount,
              companyName:res.data.companyName,
              lastVipNum:res.data.lastVipNum
            })
          }else{
            // wx.showToast({
            //   title: res.data.msg
            // })
          }
        },
        fail:(res)=>{
          wx.hideLoading();
        }
      })
    }else{
    //重新获取openid 
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: api.login,
          method:'GET',
          data:{
            code:res.code
          },
          success:(res)=>{
            wx.hideLoading();
            if(res.data.code == 200){
            wx.setStorageSync('sessionKey', res.data.data.sessionKey)
            wx.setStorageSync('openid', res.data.data.openid)
            wx.setStorageSync('wxUser',  res.data.wxUser)
            }
          },
        })
      }
    })
    }
    this.valided()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
    if(!this.valided()){
      return false
    }
    let now = util.formatTime(new Date());
    if((this.data.wxUser!=null &&this.data.wxUser.id!=null && this.data.wxUser.vipEndTime > now)){
      return {
        title: '搜呀搜VIP体验卡',
        desc: '体验成为会员，享受相应权益！',
        path: '/pages/home/index?sender='+this.data.wxUser.id // 路径，传递参数到指定页面。
       }
    }
    
    return {
      title: '搜呀搜分享',
      desc: '体验搜呀搜信息平台！',
      path: '/pages/home/index' // 路径，传递参数到指定页面。
     }
    
  }
})